package org.light.generator;

import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.domain.Util;

public class POIPPTUtilGenerator extends Util{
	public POIPPTUtilGenerator(){
		super();
		super.fileName = "POIPPTUtil.java";
	}
	
	public POIPPTUtilGenerator(String packageToken){
		super();
		this.setPackageToken(packageToken);
		super.fileName = "POIPPTUtil.java";
	}
	
	@Override
	public void setPackageToken(String packageToken) {
		this.packageToken = packageToken;
	}
	
	@Override
	public String generateUtilString() {
		StatementList sList = new StatementList();
		sList.add(new Statement(1000L,0, "package "+this.packageToken+".utils;"));
		sList.add(new Statement(2000L,0,""));

		sList.add(new Statement(3000L,0,"import java.awt.Color;"));
		sList.add(new Statement(4000L,0,"import java.awt.geom.Rectangle2D;"));
		sList.add(new Statement(5000L,0,"import java.awt.image.BufferedImage;"));
		sList.add(new Statement(6000L,0,"import java.io.ByteArrayInputStream;"));
		sList.add(new Statement(7000L,0,"import java.io.IOException;"));
		sList.add(new Statement(8000L,0,"import java.io.InputStream;"));
		sList.add(new Statement(9000L,0,"import java.io.OutputStream;"));
		sList.add(new Statement(10000L,0,"import java.util.List;"));
		sList.add(new Statement(11000L,0,"import java.util.Map;"));
		sList.add(new Statement(12000L,0,"import java.util.TreeMap;"));
		sList.add(new Statement(13000L,0,""));
		sList.add(new Statement(14000L,0,"import javax.imageio.ImageIO;"));
		sList.add(new Statement(15000L,0,""));
		sList.add(new Statement(16000L,0,"import org.apache.poi.sl.usermodel.TableCell;"));
		sList.add(new Statement(17000L,0,"import org.apache.poi.sl.usermodel.TextParagraph.TextAlign;"));
		sList.add(new Statement(18000L,0,"import org.apache.poi.sl.usermodel.VerticalAlignment;"));
		sList.add(new Statement(19000L,0,"import org.apache.poi.xslf.usermodel.XMLSlideShow;"));
		sList.add(new Statement(20000L,0,"import org.apache.poi.xslf.usermodel.XSLFPictureData;"));
		sList.add(new Statement(21000L,0,"import org.apache.poi.xslf.usermodel.XSLFPictureShape;"));
		sList.add(new Statement(22000L,0,"import org.apache.poi.xslf.usermodel.XSLFSlide;"));
		sList.add(new Statement(23000L,0,"import org.apache.poi.xslf.usermodel.XSLFTable;"));
		sList.add(new Statement(24000L,0,"import org.apache.poi.xslf.usermodel.XSLFTableCell;"));
		sList.add(new Statement(25000L,0,"import org.apache.poi.xslf.usermodel.XSLFTableRow;"));
		sList.add(new Statement(26000L,0,"import org.apache.poi.xslf.usermodel.XSLFTextBox;"));
		sList.add(new Statement(27000L,0,"import org.apache.poi.xslf.usermodel.XSLFTextParagraph;"));
		sList.add(new Statement(28000L,0,"import org.apache.poi.xslf.usermodel.XSLFTextRun;"));
		sList.add(new Statement(29000L,0,""));
		sList.add(new Statement(30000L,0,"public final class POIPPTUtil {"));
		sList.add(new Statement(31000L,1,"public static BufferedImage getBufferedImage(byte[] image) {"));
		sList.add(new Statement(32000L,2,"BufferedImage bi = null;"));
		sList.add(new Statement(33000L,2,"try {"));
		sList.add(new Statement(34000L,3,"InputStream buffin = new ByteArrayInputStream(image);"));
		sList.add(new Statement(35000L,3,"bi = ImageIO.read(buffin);"));
		sList.add(new Statement(36000L,2,"} catch (IOException e) {"));
		sList.add(new Statement(37000L,3,"e.printStackTrace();"));
		sList.add(new Statement(38000L,2,"}"));
		sList.add(new Statement(39000L,2,"return bi;"));
		sList.add(new Statement(40000L,1,"}"));
		sList.add(new Statement(41000L,0,""));
		sList.add(new Statement(42000L,1,"private static boolean containsImage(List<Boolean> isImages) {"));
		sList.add(new Statement(43000L,2,"for (Boolean isImage : isImages) {"));
		sList.add(new Statement(44000L,3,"if (isImage)"));
		sList.add(new Statement(45000L,4,"return true;"));
		sList.add(new Statement(46000L,2,"}"));
		sList.add(new Statement(47000L,2,"return false;"));
		sList.add(new Statement(48000L,1,"}"));
		sList.add(new Statement(49000L,1,""));
		sList.add(new Statement(50000L,1,"public static void exportPPTWithImage(OutputStream out, String pptName, List<String> headers,"));
		sList.add(new Statement(51000L,3,"List<List<Object>> contents, List<Boolean> isImages) throws Exception {"));
		sList.add(new Statement(52000L,2,"XMLSlideShow ppt = new XMLSlideShow();"));
		sList.add(new Statement(53000L,2,"for (int i=0; i< contents.size();i+=5) {"));
		sList.add(new Statement(54000L,3,"int start = i;"));
		sList.add(new Statement(55000L,3,"int end = i+5 >contents.size()?contents.size():i+5;"));
		sList.add(new Statement(56000L,3,"exportPPTWithImageSlide(ppt,headers,contents.subList(start, end),isImages);"));
		sList.add(new Statement(57000L,2,"}"));
		sList.add(new Statement(58000L,2,"ppt.write(out);"));
		sList.add(new Statement(59000L,1,"}"));
		sList.add(new Statement(60000L,1,""));
		sList.add(new Statement(61000L,1,"public static void exportPPTWithImageSlide(XMLSlideShow ppt, List<String> headers,"));
		sList.add(new Statement(62000L,3,"List<List<Object>> contents, List<Boolean> isImages) throws Exception {"));
		sList.add(new Statement(63000L,2,""));
		sList.add(new Statement(64000L,2,"XSLFSlide slide = ppt.createSlide();//创建幻灯片"));
		sList.add(new Statement(65000L,2,""));
		sList.add(new Statement(66000L,2,"XSLFTable table = slide.createTable();//创建表格"));
		sList.add(new Statement(67000L,2,"table.setAnchor(new Rectangle2D.Double(10, 30, 0, 0));"));
		sList.add(new Statement(68000L,2,"XSLFTableRow tableRow0 = table.addRow(); //创建表格行"));
		sList.add(new Statement(69000L,2,"for(int j = 0; j < headers.size(); j++) {"));
		sList.add(new Statement(70000L,3,"XSLFTableCell tableCell = tableRow0.addCell();//创建表格单元格"));
		sList.add(new Statement(71000L,3,"XSLFTextParagraph p = tableCell.addNewTextParagraph();"));
		sList.add(new Statement(72000L,3,"p.setTextAlign(TextAlign.CENTER);"));
		sList.add(new Statement(73000L,3,"XSLFTextRun tr = p.addNewTextRun();"));
		sList.add(new Statement(74000L,3,"tr.setText(String.valueOf(headers.get(j)));"));
		sList.add(new Statement(75000L,3,"tr.setBold(true);"));
		sList.add(new Statement(76000L,3,""));
		sList.add(new Statement(77000L,3,"tableCell.setBorderWidth(TableCell.BorderEdge.left,1);"));
		sList.add(new Statement(78000L,3,"tableCell.setBorderWidth(TableCell.BorderEdge.top,1);"));
		sList.add(new Statement(79000L,3,"tableCell.setBorderWidth(TableCell.BorderEdge.right,1);"));
		sList.add(new Statement(80000L,3,"tableCell.setBorderWidth(TableCell.BorderEdge.bottom,1);"));
		sList.add(new Statement(81000L,3,"tableCell.setBorderColor(TableCell.BorderEdge.bottom,Color.BLACK);"));
		sList.add(new Statement(82000L,3,"tableCell.setBorderColor(TableCell.BorderEdge.left,Color.BLACK);"));
		sList.add(new Statement(83000L,3,"tableCell.setBorderColor(TableCell.BorderEdge.top,Color.BLACK);"));
		sList.add(new Statement(84000L,3,"tableCell.setBorderColor(TableCell.BorderEdge.right,Color.BLACK);"));
		sList.add(new Statement(85000L,2,"}"));
		sList.add(new Statement(86000L,2,"tableRow0.setHeight(60);"));
		sList.add(new Statement(87000L,2,"for(int i = 0; i < contents.size(); i++) {"));
		sList.add(new Statement(88000L,2,"XSLFTableRow tableRow = table.addRow(); //创建表格行"));
		sList.add(new Statement(89000L,2,"for(int j = 0; j < contents.get(i).size(); j++) {"));
		sList.add(new Statement(90000L,3,"XSLFTableCell tableCell = tableRow.addCell();//创建表格单元格"));
		sList.add(new Statement(91000L,3,"XSLFTextParagraph p = tableCell.addNewTextParagraph();"));
		sList.add(new Statement(92000L,3,"XSLFTextRun tr = p.addNewTextRun();"));
		sList.add(new Statement(93000L,3,"if (!isImages.get(j)) tr.setText(String.valueOf(contents.get(i).get(j)));"));
		sList.add(new Statement(94000L,3,"else {"));
		sList.add(new Statement(95000L,4,"if (contents.get(i).get(j)!=null) {"));
		sList.add(new Statement(96000L,5,"XSLFPictureData pictureData = ppt.addPicture((byte[])contents.get(i).get(j), XSLFPictureData.PictureType.PNG);"));
		sList.add(new Statement(97000L,5,"XSLFPictureShape pic = slide.createPicture(pictureData);"));
		sList.add(new Statement(98000L,5,"pic.setAnchor(getPicRect(isImages,i,j,(byte[])contents.get(i).get(j)));"));
		sList.add(new Statement(99000L,4,"}"));
		sList.add(new Statement(100000L,3,"}"));
		sList.add(new Statement(101000L,0,""));
		sList.add(new Statement(102000L,3,"p.setTextAlign(TextAlign.CENTER);"));
		sList.add(new Statement(103000L,3,"tableCell.setVerticalAlignment(VerticalAlignment.MIDDLE);"));
		sList.add(new Statement(104000L,0,""));
		sList.add(new Statement(105000L,3,"tableCell.setBorderWidth(TableCell.BorderEdge.left,1);"));
		sList.add(new Statement(106000L,3,"tableCell.setBorderWidth(TableCell.BorderEdge.top,1);"));
		sList.add(new Statement(107000L,3,"tableCell.setBorderWidth(TableCell.BorderEdge.right,1);"));
		sList.add(new Statement(108000L,3,"tableCell.setBorderWidth(TableCell.BorderEdge.bottom,1);"));
		sList.add(new Statement(109000L,3,"tableCell.setBorderColor(TableCell.BorderEdge.bottom,Color.BLACK);"));
		sList.add(new Statement(110000L,3,"tableCell.setBorderColor(TableCell.BorderEdge.left,Color.BLACK);"));
		sList.add(new Statement(111000L,3,"tableCell.setBorderColor(TableCell.BorderEdge.top,Color.BLACK);"));
		sList.add(new Statement(112000L,3,"tableCell.setBorderColor(TableCell.BorderEdge.right,Color.BLACK);"));
		sList.add(new Statement(113000L,1,""));
		sList.add(new Statement(114000L,2,"}"));
		sList.add(new Statement(115000L,2,"tableRow.setHeight(90);"));
		sList.add(new Statement(116000L,1,"}"));
		sList.add(new Statement(117000L,2,""));
		sList.add(new Statement(118000L,1,"int colsWidth = 700 / countUnits(isImages);"));
		sList.add(new Statement(119000L,1,"for (int i=0;i<isImages.size();i++) {"));
		sList.add(new Statement(120000L,2,"if (isImages.get(i)) table.setColumnWidth(i, colsWidth*3);"));
		sList.add(new Statement(121000L,2,"else table.setColumnWidth(i, colsWidth*2);"));
		sList.add(new Statement(122000L,1,"}"));
		sList.add(new Statement(123000L,0,""));
		sList.add(new Statement(124000L,1,"}"));
		sList.add(new Statement(125000L,1,""));
		sList.add(new Statement(126000L,1,"public static int countUnits(List<Boolean> isImages) {"));
		sList.add(new Statement(127000L,2,"int count = 0;"));
		sList.add(new Statement(128000L,2,"for (Boolean b:isImages) {"));
		sList.add(new Statement(129000L,3,"if (b) count +=3;"));
		sList.add(new Statement(130000L,3,"else count += 2;"));
		sList.add(new Statement(131000L,2,"}"));
		sList.add(new Statement(132000L,2,"return count;"));
		sList.add(new Statement(133000L,1,"}"));
		sList.add(new Statement(134000L,1,""));
		sList.add(new Statement(135000L,1,"public static int countCurrentUnits(List<Boolean> isImages,int pos) {"));
		sList.add(new Statement(136000L,2,"int count = 0;"));
		sList.add(new Statement(137000L,2,"int cur = 0;"));
		sList.add(new Statement(138000L,2,""));
		sList.add(new Statement(139000L,2,"for (Boolean b:isImages) {"));
		sList.add(new Statement(140000L,3,"if (cur < pos) {"));
		sList.add(new Statement(141000L,4,"if (b) count +=3;"));
		sList.add(new Statement(142000L,4,"else count += 2;"));
		sList.add(new Statement(143000L,4,"cur ++;"));
		sList.add(new Statement(144000L,3,"}else {"));
		sList.add(new Statement(145000L,4,"return count;"));
		sList.add(new Statement(146000L,3,"}"));
		sList.add(new Statement(147000L,2,"}"));
		sList.add(new Statement(148000L,2,"return count;"));
		sList.add(new Statement(149000L,1,"}"));
		sList.add(new Statement(150000L,1,""));
		sList.add(new Statement(151000L,1,"public static Rectangle2D getPicRect(List<Boolean> isImages,int row, int col,byte[] imagebytes) {"));
		sList.add(new Statement(152000L,2,"int unitCounts = countUnits(isImages);"));
		sList.add(new Statement(153000L,2,"double unitWidth = 700.0/unitCounts;"));
		sList.add(new Statement(154000L,2,"double curX = countCurrentUnits(isImages,col);"));
		sList.add(new Statement(155000L,2,"Map<String,Integer> widHei = getImageWidthHeight(imagebytes);"));
		sList.add(new Statement(156000L,2,"int imageWidth = widHei.get(\"width\");"));
		sList.add(new Statement(157000L,2,"int imageHeight = widHei.get(\"height\");"));
		sList.add(new Statement(158000L,2,"if (imageHeight == 0) return new Rectangle2D.Double(curX*unitWidth, 90+90*row,0 , 0);"));
		sList.add(new Statement(159000L,2,"double width = 90.0/(double) imageHeight * imageWidth;"));
		sList.add(new Statement(160000L,2,"double offsetx = (3* unitWidth - width)/2;"));
		sList.add(new Statement(161000L,2,"Rectangle2D rect = new Rectangle2D.Double(curX*unitWidth+offsetx, 90+90*row,width , 90);"));
		sList.add(new Statement(162000L,2,"return rect;"));
		sList.add(new Statement(163000L,1,"}"));
		sList.add(new Statement(164000L,1,""));
		sList.add(new Statement(165000L,1,"public static Map<String,Integer> getImageWidthHeight(byte[] imagebytes){"));
		sList.add(new Statement(166000L,2,"Map<String,Integer> results = new TreeMap<>();"));
		sList.add(new Statement(167000L,0,""));
		sList.add(new Statement(168000L,2,"BufferedImage bi = getBufferedImage(imagebytes);"));
		sList.add(new Statement(169000L,2,"if (bi == null) {"));
		sList.add(new Statement(170000L,3,"results.put(\"width\", 0);"));
		sList.add(new Statement(171000L,3,"results.put(\"height\", 0);"));
		sList.add(new Statement(172000L,3,"return results;"));
		sList.add(new Statement(173000L,2,"}"));
		sList.add(new Statement(174000L,2,"Integer width = bi.getWidth();"));
		sList.add(new Statement(175000L,2,"Integer height = bi.getHeight();"));
		sList.add(new Statement(176000L,2,"results.put(\"width\", width);"));
		sList.add(new Statement(177000L,2,"results.put(\"height\", height);"));
		sList.add(new Statement(178000L,2,"return results;"));
		sList.add(new Statement(179000L,1,"}"));
		sList.add(new Statement(180000L,0,"}"));


		return sList.getContent();
	}

}
